//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.model.IDataProxy;
	import reader.model.vo.OPMLItemVO;
	import reader.utils.OPMLUtils;

	public class OPMLCommand extends SimpleCommand implements ICommand
	{
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var saveOpml:XML;
		private var proxy:IDataProxy;
		//////////////////////////////////////////////////////////////////////////////
		//methods
				
		override public function execute(notification:INotification):void
		{
			var type:String = notification.getType();
			var model:ReaderModelLocator = ReaderModelLocator.instance;
			if(type == OPMLUtils.OUTPORT_OPML)
			{
				saveOpml = OPMLUtils.outportOPML(model.blogSource, model.blogLabels, model.user.userName);
				var file:File = File.documentsDirectory;
				try
				{
					file.browseForSave(ReaderResource.instance.resource.opmlBrowseForSave);
				    file.addEventListener(Event.SELECT, onBrowseForSave_handler);
				}
				catch (error:Error)
				{
				    trace(error.message)
				}
			}
			else if(type == OPMLUtils.INPORT_OPML)
			{
				proxy = notification.getBody() as IDataProxy;
				
				var file:File = File.desktopDirectory;
				var xmlFilter:FileFilter = new FileFilter("OPML", "*.xml;*.opml;");

				try
				{
					file.browseForOpen(ReaderResource.instance.resource.opmlBrowseForOpen, [xmlFilter]);
				    file.addEventListener(Event.SELECT, onBrowseForOpen_handler);
				}
				catch (error:Error)
				{
				    trace(error.message)
				}
			}
		}
		
		private function onBrowseForSave_handler(e:Event):void
		{
			var file:File = e.target as File;
			var newFile:File = new File(file.nativePath + ".xml");
			file.removeEventListener(Event.SELECT, onBrowseForSave_handler);
			file.cancel();
			if(!saveOpml)
			{
				newFile.cancel();
				return;
			}
		    var str:String = OPMLUtils.XML_VERSION + saveOpml.toXMLString();
		    if (!newFile.exists)
		    {
		        var stream:FileStream = new FileStream();
		        stream.open(newFile, FileMode.UPDATE);
		        stream.writeUTFBytes(str);
		        stream.close();
		    }
		    saveOpml = null;

		}
		
		private function onBrowseForOpen_handler(e:Event):void
		{
			var stream:FileStream = new FileStream();
		    stream.open(e.target as File, FileMode.READ);
		    var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
		    
		    try
		    {
		    	var xml:XML = new XML(fileData);
		    }
		    catch (error:Error)
			{
			    trace(error.message)
			}
			
			var list:Vector.<OPMLItemVO> = OPMLUtils.parseOPML(xml);
			for each(var item:OPMLItemVO in list)
			{
				proxy.addListSubscribe(item.subscribe, item.label)
			}

		}
			
		
		
	}
}